# Pongathon
# Version 1.4.1
# Copyright 2014 Josef J. Polak III
# Pongathon is distributed under the terms of the GNU General Public License

# This file is part of Pongathon.

# Pongathon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pongathon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Code for the Event Manager and all events.

class EventManager:
	"""Coordinates communications between the Model (Game), views
	(Pygame, logs, etc.) and Controllers (Keyboard, CPU, clients) through 
	the use of events."""

	def __init__(self):
		from weakref import WeakKeyDictionary
		self.listeners = WeakKeyDictionary()
		self.eventQueue = []

	def RegisterListener(self, listener):
		self.listeners[listener] = 1

	def UnregisterListener(self, listener):
		if listener in self.listeners:
			del self.listeners[listener]
	
	def Post(self, event):
		"""Spam everyone with event"""
		for listener in self.listeners:
			listener.Notify(event)

# What follows is a list of all events. None of these classes should perform 
#  any tasks, as that could introduce vulnerabilities if and when I write the
#  netcode. (Pointed out in sjbrown's Pygame tutorial).

class Event:
	"""Generic event superclass."""

	def __init__(self):
		self.name = "Generic Event"
	
	def __str__(self):
		return '<%s %s>' % (self.__class__.__name__, id(self))

class Tick(Event):
	def __init__(self):
		self.name = "CPU Tick Event"

class Quit(Event):
	def __init__(self):
		self.name = "Program Quit Event"

class BallCollision(Event):
	def __init__(self, object):
		self.name = "Ball Collision Event"
		self.object = object

class PaddleMoveRequest(Event):
	def __init__(self, player, direction):
		self.name = "Paddle Movement Request"
		self.player = player
		self.direction = direction

class SetStateRequest(Event):
	def __init__(self, state):
		self.name = "Set State Event"
		self.state = state

class PlayerScored(Event):
	def __init__(self, num):
		self.name = "Player Scored Event"
		self.num = num

class GameStartRequest(Event):
	def __init__(self, num):
		self.name = "Game Start Request"
		self.num = num

class SetThemeRequest(Event):
	def __init__(self, num):
		self.name = "Set Theme Request"
		self.num = num

class DisplayModeRequest(Event):
	def __init__(self, res=(640, 480), full=False):
		self.name = "Display Mode Request"
		self.res = res
		self.full = full
